<?php
class Admin_EatmedicineController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;

        $tWhere = ' 1 and leave_status = 2';

        $tSMO = new SeniorModel;
        $tMMO = new MedicineModel;
        $tMLMO = new MedicinelibraryModel;
        $tWMO = new WorkerModel;
        $tAMO = new AdminModel;

        if(!empty($_COOKIE['admin']['roles'])){
            if($_COOKIE['admin']['roles'] != 1 && $_COOKIE['admin']['roles'] != 2){
                $tSql = 'select w.department department from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$_SESSION['admin']['id'];
                $tAData = $tAMO->query($tSql);
                $tWhere = ' 1 and leave_status = 2 and s.department = '.$tAData[0]['department'];
            }
        }
        $tSql = 'select count(*) c from '.$tSMO->table.' s left join '.$tMMO->table.' m on m.sid = s.id left join '.$tMLMO->table.' ml on ml.id = m.ml_id where '.$tWhere;
        $tCount = $tSMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);

        if(!empty($tCnt)){
            $tStr = '';
            $tDatas = $tSMO->field('id sid,name')->where($tWhere)->order('id desc')->limit($tPage->limit())->fList();
            //查询老人用药信息
            foreach($tDatas as $tKey=>$tRow){
                $tSql = 'select GROUP_CONCAT(ml.name) medicine_name,m.sid sid,m.username username,m.remarks remarks from '.$tMMO->table.' m left join '.$tMLMO->table.' ml on ml.id = m.ml_id where m.sid = '.$tRow['sid'];
                $tMDatas = $tMMO->query($tSql);
                $tDatas[$tKey]['medicine_name'] = $tMDatas[0]['medicine_name'];
                $tDatas[$tKey]['remarks'] = $tMDatas[0]['remarks'];
                $tDatas[$tKey]['username'] = $tMDatas[0]['username'];
            }
        }

        $this->assign('tShow',$tPage->show());
        $this->assign('tDatas',$tDatas);
    
    }
    public function addAction(){
        $p = $_REQUEST;
        $pMid = empty($p['mId'])?'':Tool_Fnc::safe_string($p['mId']);

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pSid = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID错误'):Tool_Fnc::safe_string($p['sid']);
            $pMedicinenames = $p['medicine_name'];
            $pNums = $p['num'];
            $pAts = $p['at'];
            $pRemarkss = $p['remarks'];
            $pUnits = $p['unit'];
            $tMMO = new MedicineModel;
            $tMMO->begin();
            foreach ($pMedicinenames as $key => $pMedicinename) { 
                $pNums[$key] = empty($pNums[$key])?Tool_Fnc::ajaxMsg('药品数量不能为空'):$pNums[$key];
                $pAts[$key] = empty($pAts[$key])?Tool_Fnc::ajaxMsg('用药时间不能为空'):$pAts[$key];
                $pRemarkss[$key] = empty($pRemarkss[$key])?Tool_Fnc::ajaxMsg('医嘱不能为空'):$pRemarkss[$key];
                $pUnits[$key] = empty($pUnits[$key])?Tool_Fnc::ajaxMsg('药品单位不能为空'):$pUnits[$key];
                $pMedicinename = empty($pMedicinename)?Tool_Fnc::ajaxMsg('药品名称不能为空'):$pMedicinename;
                $fanqian = empty($p['fanqian'.$key])?Tool_Fnc::ajaxMsg('请选择饭前或者饭后'):$p['fanqian'.$key];
            $tType = 1;
            if($pAt[$key] == 7){$tType = 2;}

            $tTime = time();

            
            $tSql = 'select id from '.$tMMO->table.' where sid = '.$pSid.' and ml_id = '.$pMedicinename.' and num = '.$pNums[$key].' and at = '.$pAts[$key];
            $tMData = $tMMO->query($tSql);
            if(!empty($tMData[0]['id'])){
                Tool_Fnc::ajaxMsg('记录已存在');
            }

            $tData = array(
                'sid' => $pSid,
                'ml_id' => $pMedicinename,
                'status' => 0,
                'date' => date('Y-m-d',$tTime),
                'at' => $pAts[$key],
                'num' => $pNums[$key],
                'unit' => $pUnits[$key],
                'remarks' => $pRemarkss[$key],
                'created' => $tTime,
                'type' => $tType, 
                'fanqian' => $fanqian,
                'username' => $_SESSION['admin']['name'],
            );

            if(!$tId = $tMMO->insert($tData)){
                Tool_Fnc::ajaxMsg('添加失败');
                $tMMO->back();
            }
        }
            $tMMO->commit();
            Tool_Fnc::ajaxMsg('添加成功',1);
        }
        //药名显示
        $tMLMO = new MedicinelibraryModel;
        $tMLDatas = $tMLMO->field('id,name')->fList();
        //老人姓名显示
        $tSMO = new SeniorModel;
        $tSDatas = $tSMO->field('id,name')->where('leave_status = 2')->fList();
    
        if(!empty($pMid)){
            $tData = $tMLMO->field('img')->where('id = '.$pMid)->fRow();
            Tool_Fnc::ajaxMsg('',1,$tData);
        }

        $this->assign('tSDatas',$tSDatas);
        $this->assign('tMLDatas',$tMLDatas);
    }
    public function editAction(){
        $p = $_REQUEST;
        $pSid = empty($p['sid'])?Tool_Fnc::ajaxMsg('SID错误'):Tool_Fnc::safe_string($p['sid']);

        $tMMO = new MedicineModel;
        $tMLMO = new MedicinelibraryModel;
        $tSMO = new SeniorModel;

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pMid = empty($p['mid'])?Tool_Fnc::ajaxMsg('MID错误'):Tool_Fnc::safe_string($p['mid']);
            $pSid = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID错误'):Tool_Fnc::safe_string($p['sid']);
            $pMedicinename = empty($p['medicine_name'])?Tool_Fnc::ajaxMsg('请选择药品名称'):Tool_Fnc::safe_string($p['medicine_name']);
            $pNum = empty($p['num'])?Tool_Fnc::ajaxMsg('请选择用量'):Tool_Fnc::safe_string($p['num']);
            $pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('请选择用药时间'):Tool_Fnc::safe_string($p['at']);
            $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);
            $pUnit = empty($p['unit'])?Tool_Fnc::ajaxMsg('请选择单位'):Tool_Fnc::safe_string($p['unit']);

            $tType = 1;

            if($pAt == 7){$tType = 2;}

            $tTime = time();

            $tSql = 'select id from '.$tMMO->table.' where sid = '.$pSid.' and ml_id = '.$pMedicinename.' and num = '.$pNum.' and at = '.$pAt;

            $tData = array(
                'id' => $pMid,
                'sid' => $pSid,
                'ml_id' => $pMedicinename,
                'status' => 0,
                'num' => $pNum,
                'unit' => $pUnit,
                'date' => date('Y-m-d',$tTime),
                'at' => $pAt,
                'remarks' => $pRemarks,
                'type' => $tType,
                'updated' => $tTime,
                'username' => $_SESSION['admin']['name'],
            );
            if(!$tMMO->update($tData)){
                Tool_Fnc::ajaxMsg('修改失败');
            }
            Tool_Fnc::ajaxMsg('修改成功',1);
        }
        //编辑页面显示
        $tSql = 'select m.id mid,s.name s_name,ml.name name,m.sid sid,m.ml_id ml_id,m.remarks remarks,fanqian,m.num num,m.unit unit,m.at at,ml.img img from '.$tMMO->table.' m left join '.$tMLMO->table.' ml on m.ml_id = ml.id left join '.$tSMO->table.' s on m.sid = s.id where m.sid = '.$pSid;
        $tMDatas = $tMMO->query($tSql);
        //老人信息显示
        $tSDatas = $tSMO->field('id,name')->fList();
        //药名显示
        $tMLDatas = $tMLMO->field('id,name')->fList();

        $this->assign('pSid',$pSid);
        $this->assign('tMDatas',$tMDatas);
        $this->assign('tSDatas',$tSDatas);
        $this->assign('tMLDatas',$tMLDatas);
    
    }
    public function delAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('id不能为空'):Tool_Fnc::safe_string($p['id']);

        $tMO = new MedicineModel;
        if(!$tMO->del($pId)){
            Tool_Fnc::ajaxMsg('删除失败');
        }
        Tool_Fnc::ajaxMsg('删除成功',1);
    }
}
?>
